/**  

* @Title: LoginAction.java 

* @Package com.gxbadge.action 

* @Description: TODO(用一句话描述该文件做什么) 

* @author CST

* @version V1.0  

*/ 

package com.gxbadge.action;

import java.io.PrintWriter;
import java.sql.Timestamp;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.gxbadge.action.base.BaseAction;
import com.gxbadge.model.Administrator;
import com.gxbadge.util.DateTimeUtil;
import com.gxbadge.util.MD5;
import com.opensymphony.xwork2.ActionContext;

public class LoginAction extends BaseAction {
	
	private Administrator admin;

	public Administrator getAdmin() {
		return admin;
	}

	public void setAdmin(Administrator admin) {
		this.admin = admin;
	}
	
	private String picValidate;
	
	public String getPicValidate() {
		return picValidate;
	}
	public void setPicValidate(String picValidate) {
		this.picValidate = picValidate;
	}
	
	private String tip;
	
	public String getTip() {
		return tip;
	}

	public void setTip(String tip) {
		this.tip = tip;
	}

	public String checkCode() throws Exception {
		
		ActionContext ctx = ActionContext.getContext();
		
        HttpServletResponse res = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);   

		if(this.getPicValidate().trim()!=null&&!this.getPicValidate().equals("")){
			
			 String sPicValidate = ctx.getSession().get("picValidate").toString();
			 res.setContentType("text/html;charset=UTF-8");
			 PrintWriter w = res.getWriter();
			 
			 if(!this.getPicValidate().equals(sPicValidate)){
				 
	             String tip="error";
	             w.println(tip);   
			 }else{

				 String tip="suc";
	             w.println(tip); 
			 }
			 w.close();
			 
		}

		return "checkCode_success";
	}
	public String login_admin() throws Exception {
		
		if(this.getAdmin()!=null){
			
			if(this.getTip()!=null){
				this.setTip(null);
			}
			ActionContext ctx = ActionContext.getContext();
			String password = MD5.encoderByMd5(this.getAdmin().getPassword());
			Administrator admin = adminManager.findAdminByNameAndPass(this.getAdmin().getName(), password);
			if(admin!=null){
				
				Timestamp loginDate = DateTimeUtil.getNowTime();
				admin.setLastLoginDate(loginDate);
				adminManager.modify_adminByAdmin(admin);
				ctx.getSession().put("admin", admin);
				return "admin_login_success";
			}else{
				this.setTip("用户名或密码错误，请重新输入");
				return "admin_login_error";
			}

		}

		return "admin_login_error";
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}
